﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Exentials.Xoox.Spreadsheet;

namespace WebSample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            XSpreadsheet xlsx = new XSpreadsheet();
            XWorksheet sheet = xlsx.Workbook.Sheets.AddWorksheet("Sheet by XOOX");

            Reference cell = new Reference(1, 1);

            sheet[cell].SetContent("Sum of random generated numbers");
            sheet.Merge(new Reference(1, 1, 1, 7));
            sheet.Merge(new Reference(1, 8, 1, 9));

            sheet[1, 8].SetContent(DateTime.Now);

            for (int column = 1; column < cell.Column + 10; column++)
            {
                sheet[2, column].SetContent(string.Format("Title{0}", column));
            }

            Random rnd = new Random(100);
            for (int row = 3; row < 13; row++)
                for (int column = 1; column <= 10; column++)
                {
                    sheet[row, column].SetContent(rnd.NextDouble() * 100);
                }

            for (int column = 1; column <= 10; column++)
            {
                sheet[14, column].SetContent(Formula.Get.Ref(new Reference(3, column, 12, column)).Sum());
            }        

            //Set Headers
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //Response.ContentType = "application/x-msdownload";
            Response.AppendHeader("Content-Disposition", "attachment;filename=Samlpe.xlsx");
            xlsx.WriteTo(Response.OutputStream);

            Response.End();
        }
    }
}
